﻿#include "topo_info.h"
#include "qapplication.h"
#include "plug_info.h"
#include "com_rs232.h"
#include "com_manager.h"
#include "qdebug.h"

#include <QDir>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QTime>

TopoInfo * TopoInfo::instance = NULL;/*
int TopoInfo::node_count =0;
QMap<int, TOPO_NODE *> TopoInfo::node_map;*/

TopoInfo::TopoInfo() {}
TopoInfo * TopoInfo::Instance()
{
    if ( instance == NULL)
        instance = new TopoInfo;

    return instance;

}


void TopoInfo::query_node_info()
{
#ifdef TEST

    extern void create_test_data();
    create_test_data();

#endif
    QString com_name = com_manager::instance()->curComName;//comList.first();
    rs232com* rs232=   com_manager::instance()->get_com(com_name);
    if (rs232 == NULL)
        return;

    if (rs232->myCom||!rs232->myCom->isOpen())
    {
        bool opened=rs232->openPort(com_name, BaudRateType::BAUD115200, DataBitsType::DATA_8, ParityType::PAR_NONE, StopBitsType::STOP_1);
        if (!opened)
            return  ;
    }

    TopoInfo::node_map.clear();


    QTime qTime;
    qTime.start();
    rs232->query_node_online();
    qDebug()<<"query_node_online"   << "  " << qTime.elapsed()<<"ms";

    qDebug()<<qTime.elapsed()<<"ms";

    foreach(int id, TopoInfo::node_map.keys() )
   // for  ( int id = 1; id < 255; id ++)//,  TopoSetting::node_map.keys() )
    {
        if( TopoInfo::node_map.contains(id) )
        {
            int phy_cnt= rs232->query_node_phy_count(id);
            qDebug()<<"query_node_phy_count" << id << "  " << qTime.elapsed()<<"ms";

            if ( phy_cnt > 0)
            {
                rs232->query_node_phy_info(id,0);
                qDebug()<<"query_node_phy_info" << id << "  " << qTime.elapsed()<<"ms";
                rs232->query_node_phy_target(id,0);
                qDebug()<<"query_node_phy_target" << id << "  " << qTime.elapsed()<<"ms";
            }

            if ( phy_cnt > 1)
            {
                rs232->query_node_phy_info(id,1);
                qDebug()<<"query_node_phy_info" << id << "  " << qTime.elapsed()<<"ms";
                rs232->query_node_phy_target(id,1);
                qDebug()<<"query_node_phy_target" << id << "  " << qTime.elapsed()<<"ms";
            }
        }
       // qDebug()<<"query_node_info" << id << "  " << qTime.elapsed()<<"ms";
    }


}


bool TopoInfo::loadSetting()
{
   // if (load_topo_setting() == false)
    {
        init_topo_default(); //initial default
    }

    save_topo_setting();

    return false;
}

void TopoInfo::init_topo_default()
{
    node_map.clear();
    int cnt = 3;
    for ( int i = 0; i < cnt; i++)
    {
        TOPO_NODE * node = new TOPO_NODE;
        node->_id = i;

        TOPO_PHY * phy_1 = new TOPO_PHY( node->_id ,0 );
        phy_1->target = new TOPO_PHY( (i-1 + cnt)% cnt,  1);
        node->phy[0]= phy_1;

         TOPO_PHY * phy_2 = new TOPO_PHY(  node->_id, 1 );
        phy_2->target = new TOPO_PHY((i+1 + cnt) % cnt, 0);
         node->phy[1] = phy_2;

        node_map[node-> _id] = node;
    }
}

bool TopoInfo::load_topo_setting()
{
    return false;
}

void TopoInfo::save_topo_setting()
{
    QDir dir(qApp->applicationDirPath());
    if (!dir.exists("data"))
        dir.mkpath("data");

    //QJsonArray arrayNet;
    QJsonObject arrayMap;

    int i = 1;

    QMap<int, TOPO_NODE *>::Iterator  it = node_map.begin();

    while  (it !=  node_map.end())
    {
        TOPO_NODE * node = *it;
        QJsonObject objNode;
        objNode["node_id"] = node->_id;
        objNode["mac_id"] = node->mac_id ;
        objNode["com"] = node->comName;

        foreach( TOPO_PHY* phy, node->phy.values())
        {
            if ( phy!=nullptr)
            {
                QJsonObject objPhy_1 ;
                objPhy_1["node"] = phy->_node;
                objPhy_1["phy"] = phy->_phy;
                if ( phy->target)
                {
                    objPhy_1["connect_node"] = phy->target->_node ;
                    objPhy_1["connect_phy"] = phy->target->_node ;
                    objNode["phy_1"] = objPhy_1;
                }
            }
        }

        arrayMap[QString("node %1").arg( node-> _id)] = objNode;
        it++;
    }


    QByteArray bytes = QJsonDocument(arrayMap).toJson();

    QFile fileArray(QApplication::applicationDirPath() + "/data/topo_setting.json");
    if (fileArray.open(QIODevice::WriteOnly))
    {
        fileArray.write(bytes);
        fileArray.close();
    }
}
